package com.itheima.mapper;

import com.itheima.pojo.Emp;
import com.itheima.pojo.QueryParam;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface EmpMapper {
    /*
    分页1：返回总记录数total
     */
    @Select("select count(*) from emp;")
    public Long Count();

    /*
    分页2：
      查询所有的员工及其对应的部门名称
      所有员工，所以需要左外连接
      起始记录：start = （页数 - 1）*每页记录数
      每页记录数：pageSize

     */
    @Select("select e.*,d.name as deptName from emp e left join dept d on e.dept_id = d.id limit #{start},#{pageSize};")
    public List<Emp> page(Integer start, Integer pageSize);

    /*
    * PageHelper插件实现
    * 使用次插件，编写的SQL语句结尾不要加分号  ；
    * */
//    @Select("select e.*,d.name as deptName from emp e left join dept d on e.dept_id = d.id")
    public List<Emp> list(QueryParam queryParam);

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into emp (username, name, gender, phone, job, salary, image, entry_date, dept_id, create_time, update_time) " +
            "values (#{username},#{name},#{gender},#{phone},#{job},#{salary},#{image},#{entryDate},#{deptId},#{createTime},#{updateTime});")
    void insert(Emp emp);

    void deleteByIds(List<Integer> ids);

    Emp getById(Integer id);

    void updateById(Emp emp);

    /**
     * 统计各个职位的员工人数
     */
    @MapKey("pos")
    List<Map<String,Object>> countEmpJobData();
    /**
     * 统计员工性别信息
     */
    @MapKey("name")
    List<Map> countEmpGenderData();

    @Select("select * from emp;")
    List<Emp> getAll();

    @Select("select * from emp where username = #{username} and password = #{password};")
    Emp getByNameAndPassword(Emp emp);

    @Delete("delete from emp where dept_id = #{deptId};")
    void deleteByDeptId(Integer deptId);
}
